//
//  ViewController.m
//  LottieAnimationTest
//
//  Created by Felix Yin on 2019/4/4.
//  Copyright © 2019 Felix Yin. All rights reserved.
//

#import "ViewController.h"
#import "Lottie.h"
#import "PSSkiTrackingPlayer.h"

@interface ViewController ()<LOTColorValueDelegate>

@property(nonatomic,strong) LOTAnimationView *animationView;
@property(nonatomic,strong) UIImageView *bgView;
@property(nonatomic,strong) NSArray *voice;
@property(nonatomic,strong) PSSkiTrackingPlayer *voicePlayer;
@property(nonatomic,strong) NSString *currentVoice;
@property(nonatomic,assign) NSInteger currentVoiceNum;
@property(nonatomic,assign) BOOL startStatus;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.voice = @[@"ski_background",@"ski_fallraise_finish",@"ski_pause",@"ski_skilift_start"  ,@"ski_start"];
    self.currentVoiceNum = -1;
    [self loadAnimation];
}

- (void) loadAnimation{
    //    HappyBirthday
    //    5271-loading-4，02data  动画执行不了 750 1334
    [self.view addSubview:self.bgView];
    self.bgView.frame = CGRectMake(0, 0, 375, 667);
    LOTAnimationView *animationView = [LOTAnimationView animationNamed:@"vcTransition1"];
    animationView.frame = CGRectMake(0, 0, 375, 667);
    animationView.loopAnimation = NO;
    animationView.contentMode = UIViewContentModeScaleAspectFit;
    self.animationView = animationView;
    self.animationView.animationProgress = 0;
//    [self.animationView setValueDelegate:self forKeypath:[LOTKeypath keypathWithString:@"**.Color"]];
    [self.view addSubview:animationView];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    self.startStatus = YES;
    [self.animationView play];
}

//    MARK: Animation Delegate

- (CGColorRef)colorForFrame:(CGFloat)currentFrame startKeyframe:(CGFloat)startKeyframe endKeyframe:(CGFloat)endKeyframe interpolatedProgress:(CGFloat)interpolatedProgress startColor:(CGColorRef)startColor endColor:(CGColorRef)endColor currentColor:(CGColorRef)interpolatedColor{
//    NSLog(@"😄😄😄currentFrame == %@ startKeyframe == %@  endKeyframe == %@ interpolatedProgress == %@ startColor == %@ endColor == %@ currentColor == %@",@(currentFrame),@(startKeyframe),@(endKeyframe),@(interpolatedProgress),[UIColor colorWithCGColor:startColor],[UIColor colorWithCGColor:endColor],[UIColor colorWithCGColor:interpolatedColor]);
    //25
    if (!self.startStatus) {
        return interpolatedColor;
    }
    //开启动画之后再进行声音播放控制
    NSInteger frequency = 25.0;
    NSInteger currentTime = floor(currentFrame/frequency);
    if (self.currentVoiceNum != currentTime) {
        if (currentTime > (self.voice.count-2)) {
            return interpolatedColor;
        }else{
          self.currentVoiceNum = currentTime;
            dispatch_async(dispatch_get_main_queue(), ^{
                NSString *voiceName = @"测试音频";//[self.voice objectAtIndex:currentTime];
                [self.voicePlayer playAudio:voiceName distance:1 time:1];
            });
        }
    }
    return interpolatedColor;
}

//    MARK: Lazy Loading

- (UIImageView *)bgView{
    if (!_bgView) {
        _bgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"img_bg"]];
    }
    return _bgView;
}

- (PSSkiTrackingPlayer *)voicePlayer{
    if (!_voicePlayer) {
        _voicePlayer = [[PSSkiTrackingPlayer alloc] init];
    }
    return _voicePlayer;
}


@end
